package com.geek.framework.study.aquickstart.classloader;

import java.io.IOException;
import java.io.InputStream;

public class ClassLoaderTest {

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        ClassLoader classLoader = new ClassLoader(){
            @Override
            public Class<?> loadClass(String name) throws ClassNotFoundException {
                String fileName = name.substring(name.lastIndexOf(".") + 1) + ".class";
                try {
                    InputStream inputStream = getClass().getResourceAsStream(fileName);
                    if (inputStream == null) {
                        return super.loadClass(name);
                    }
                    byte[] bytes = new byte[inputStream.available()];
                    inputStream.read(bytes);
                    return defineClass(name, bytes, 0, bytes.length);
                } catch (IOException e) {
                    throw new ClassNotFoundException(e.getMessage());
                }

            }
        };

        final String classFullName = "com.geek.framework.config.demo.demo.classloader.ClassLoaderTest";
        Object obj = classLoader.loadClass(classFullName).newInstance();

        System.out.println("obj.getClass() = " + obj.getClass());
        System.out.println("flag = " + (obj instanceof ClassLoaderTest));
        System.out.println("obj.getClass().getClassLoader() = " + obj.getClass().getClassLoader());
    }



}
